//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Surface.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Domain.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::CSurface::CSurface (boost::shared_ptr<QuantLib::Surface>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurface = NULL;
#endif
	_ppSurface = &childNative;
    
}
Cephei::QL::Math::CSurface::CSurface (QuantLib::Surface& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSurface = NULL;
#endif
	_ppSurface = new boost::shared_ptr<QuantLib::Surface> (&childNative);
    
    _SurfaceOwner = owner;
    
}

Cephei::QL::Math::CSurface::CSurface (CSurface^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSurface = new boost::shared_ptr<QuantLib::Surface> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CSurface::CSurface (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurface = NULL;
#endif
	if (!t->IsSubclassOf(CSurface::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CSurface::CSurface (QuantLib::Handle<QuantLib::Surface>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSurface = &childNative;
	_ppSurface = &static_cast<boost::shared_ptr<QuantLib::Surface>>(childNative.currentLink());
    
    _SurfaceOwner = owner;
}
Cephei::QL::Math::CSurface::CSurface (QuantLib::Handle<QuantLib::Surface> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSurface = &childNative;
	_ppSurface = &static_cast<boost::shared_ptr<QuantLib::Surface>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CSurface::CSurface (QuantLib::Surface childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurface = NULL;
#endif
	_ppSurface = new boost::shared_ptr<QuantLib::Surface> (new QuantLib::Surface (childNative));
    
}
#endif

Cephei::QL::Math::CSurface::~CSurface ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSurface != NULL)
    {
	    delete _ppSurface;
        _ppSurface = NULL;
    }
}
Cephei::QL::Math::CSurface::!CSurface ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSurface != NULL)
    {
	    delete _ppSurface;
    }
}
QuantLib::Surface& Cephei::QL::Math::CSurface::GetReference ()
{
    if (_ppSurface == NULL) throw gcnew NativeNullException ();
	return **_ppSurface;
}
boost::shared_ptr<QuantLib::Surface>& Cephei::QL::Math::CSurface::GetShared ()
{
    if (_ppSurface == NULL) throw gcnew NativeNullException ();
	return *_ppSurface;
}
QuantLib::Surface* Cephei::QL::Math::CSurface::GetPointer ()
{
    if (_ppSurface == NULL) throw gcnew NativeNullException ();
	return &**_ppSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Surface>& Cephei::QL::Math::CSurface::GetHandle ()
{
	if (_phSurface == NULL)
	{
		_phSurface = new Handle<QuantLib::Surface> (*_ppSurface);
	}
	return *_phSurface;
}
#endif
bool Cephei::QL::Math::CSurface::HasNative () 
{
	return (_ppSurface != NULL);
}

Cephei::QL::Math::IDomain^ Cephei::QL::Math::CSurface::Domain::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Domain> _rv = (boost::shared_ptr<QuantLib::Domain>)(*_ppSurface)->domain ( );   
        Cephei::QL::Math::CDomain^ _nrv = gcnew Cephei::QL::Math::CDomain (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

